import Ref from "./Ref.js";
import Line from "./RefLine.js";

/**
 * @implements RefI<number>
 */
export default class RefNumber extends Ref {

  /** @type { Ref<number> } */
  ref;
  /** @type { Line } */
  line;

  /**
   *
   * @param { number } value
   * @param { number } length
   */
  constructor(value, length) {
    super();
    this.ref = new Ref(value);
    this.line = new Line(value, length);
  }

  get value() {
    return this.ref.value;
  }

  set value(val) {
    const changing = this.line.changing;
    this.ref.value = val;
    this.line.value = val;
    const changeValue = () => {
      requestAnimationFrame(() => {
        this.line.trigger();
        this.ref.value = this.line.value;
        if (this.line.changing) {
          changeValue();
        }
      });
    }
    if (!changing) {
      changeValue();
    }
  }

  /**
   * @param { (newVal: any, oldVal: any) => void } func
   * @param { boolean } [deep]
   */
  bind(func, deep) {
    this.ref.bind(func, deep);
  }
}
